Option Strict On

#Region "Using Directives"

Imports System
Imports System.Text

#End Region

Namespace C4F.DevKit.Bluetooth

    Namespace ServiceAndDeviceDiscovery

        ''' <summary>
        ''' Creates an endpoint for a service.
        ''' </summary>
        Public Class BluetoothEndPoint
            Inherits System.Net.EndPoint

#Region "Private Members"

            ''' <summary>
            ''' Represents the device address.
            ''' </summary>

            Private m_address As BluetoothSocketAddress

            ''' <summary>
            ''' Represents the guid of the service on remote device.
            ''' </summary>
            Private servClassGuid As Guid

            ''' <summary>
            ''' Represents the port number.
            ''' </summary>
            Private portNo As Integer

#End Region

#Region "Constructors"

            ''' <summary>
            ''' Initializes a Bluetooth endpoint object.
            ''' </summary>
            Public Sub New()
            End Sub

            ''' <summary>
            ''' Initializes a Bluetooth endpoint object.
            ''' </summary>
            ''' <param name="device">Represents the remote device to connect to.</param>
            ''' <param name="serviceClassGuid">Represents the guid of the service
            ''' on remote device.</param>
            Public Sub New(ByVal device As Device, ByVal serviceClassGuid As Guid)

                With Me
                    .m_address = device.Address
                    .servClassGuid = serviceClassGuid
                    .portNo = BluetoothHelper.ANY_CHANNEL
                End With

            End Sub

            ''' <summary>
            ''' Initializes a Bluetooth endpoint object.
            ''' </summary>
            ''' <param name="deviceAddress">Represents the remote device address.
            ''' </param>
            ''' <param name="serviceClassGuid">Represents the guid of the service
            ''' on remote device.</param>
            Public Sub New(ByVal deviceAddress As BluetoothSocketAddress, _
                ByVal serviceClassGuid As Guid)

                With Me
                    .m_address = deviceAddress
                    .servClassGuid = serviceClassGuid
                    .portNo = BluetoothHelper.ANY_CHANNEL
                End With

            End Sub

            ''' <summary>
            ''' Initializes a Bluetooth endpoint object.
            ''' </summary>
            ''' <param name="deviceAddress">Represents the remote device address.
            ''' </param>
            ''' <param name="serviceClassGuid">Represents the guid of the service
            ''' on remote device.</param>
            ''' <param name="port">Represents the port on remote device.</param>
            Public Sub New(ByVal deviceAddress As BluetoothSocketAddress, _
                ByVal serviceClassGuid As Guid, ByVal port As Integer)

                With Me
                    .m_address = deviceAddress
                    .servClassGuid = serviceClassGuid
                    .portNo = port
                End With

            End Sub

#End Region

#Region "Internal Properties"

            ''' <summary>
            ''' Gets the port number.
            ''' </summary>
            Friend ReadOnly Property Port() As Integer

                Get
                    Return portNo
                End Get

            End Property

            ''' <summary>
            ''' Gets the service class id of the service.
            ''' </summary>
            Friend ReadOnly Property ServiceClassGuid() As Guid

                Get
                    Return servClassGuid
                End Get

            End Property

            ''' <summary>
            ''' Gets the address of device.
            ''' </summary>
            Friend ReadOnly Property Address() As BluetoothSocketAddress

                Get
                    Return m_address
                End Get

            End Property

#End Region

#Region "Public Functions"

            ''' <summary>
            ''' Creates a socket address by using the port, serviceClass Guid and 
            ''' address variable values.
            ''' </summary>
            ''' <returns>Returns a socket address.</returns>
            Public Overloads Overrides Function Serialize() As System.Net.SocketAddress

                Dim socketAddress As New System.Net.SocketAddress( _
                                            System.Net.Sockets.AddressFamily.Unspecified, _
                                            BluetoothHelper.SOCKET_ADDRESS_LENGTH)

                ' AddressFamily is the same for both CE and XP.
                socketAddress(0) = 32

                ' Copy in the remote bt address.
                Dim addr() As Byte = BitConverter.GetBytes(m_address.Address)
                For i As Integer = 0 To addr.Length - 1
                    socketAddress(BluetoothHelper.ADDRESS_OFFSET + i) = addr(i)
                Next

                ' Copy in the guid of the service we want to connect with.
                Dim guidArray() As Byte = servClassGuid.ToByteArray()
                For i As Integer = 0 To 15
                    socketAddress(BluetoothHelper.GUID_OFFSET + i) = guidArray(i)
                Next

                ' Copy in the channel number that we would like this 
                ' connection to use.
                Dim portAsBytes() As Byte = BitConverter.GetBytes(portNo)
                For i As Integer = 0 To 3
                    socketAddress(BluetoothHelper.CHANNEL_OFFSET + i) = portAsBytes(i)
                Next

                Return socketAddress

            End Function

            ''' <summary>
            ''' Creates an endpoint from the specified socket address.
            ''' </summary>
            ''' <param name="socketAddress">The socket address that serves as 
            ''' the endpoint for a connection.</param>
            ''' <returns>A new EndPoint instance that is initialized from the 
            ''' specified SocketAddress instance.</returns>
            Public Overloads Overrides Function Create(ByVal socketAddress As System.Net.SocketAddress) _
                                                As System.Net.EndPoint

                ' Device Address.
                Dim addr(7) As Byte
                For i As Integer = 0 To 7
                    addr(i) = socketAddress(BluetoothHelper.ADDRESS_OFFSET + i)
                Next

                ' Guid.
                Dim serviceClassGuid(15) As Byte
                For i As Integer = 0 To 15
                    serviceClassGuid(i) = socketAddress(BluetoothHelper.GUID_OFFSET + i)
                Next

                Dim deviceAddr() As Byte = addr
                Dim guidValue As New Guid(serviceClassGuid)
                ' Port number.
                Dim portNumArray(3) As Byte
                For i As Integer = 0 To 3
                    portNumArray(i) = socketAddress(BluetoothHelper.CHANNEL_OFFSET + i)
                Next

                Dim bthAddr As New BluetoothSocketAddress()

                With bthAddr
                    .AddressFamily = CUShort(socketAddress.Family)
                    .Port = CUInt(BitConverter.ToInt32(portNumArray, 0))
                    .ServiceClassId = guidValue
                    .Address = CULng(BitConverter.ToInt64(addr, 0))
                End With

                Return New BluetoothEndPoint(bthAddr, guidValue, CInt(bthAddr.Port))

            End Function

#End Region

        End Class

    End Namespace

End Namespace
